require 'fileutils'
require 'ftools'

def resizeFile(filename)
   	if File.extname(filename) == '.png' then
   		s2 = filename.split('/', 2)[1]
   		s3 = File.split(s2)[0]
   		
   		dir = ARGV[1]+'/'+ s3
   		if s3 == '.' then
   			dir = ARGV[1];
   		end
   		
   		FileUtils.mkdir_p(dir)
   		
   		command = "convert \"#{filename}\" -resize 50%  \"#{ARGV[1]+'/'+s2}\""
   		system( command );
   	end
end

def resizeInDir(dirmane)
	Dir.foreach(dirmane) do |entry|
   		if  entry != '.' && entry != '..' then 	
   			if File.directory?(dirmane+'/'+entry) then
   				resizeInDir(dirmane+'/'+entry)
   			else
				resizeFile(dirmane+'/'+entry)
   			end
   		end
	end
end

if ARGV[1] != nil && ARGV[0] != nil && ARGV[0] != '.' && ARGV[0] != '..' && ARGV[0] != './' && ARGV[0] != '../' then
	resizeInDir(ARGV[0])
end